/*
 * JSettingInitializationServer.java
 *
 * Created on 20-ott-2008, 17.47.56
 */
package monopoli.gui;

import monopoli.Monopoli;
import monopoli.MonopoliView;
import monopoli.communication.Communication;


/**
 *
 * @author Marco
 */
public class JSettingInitializationServer extends javax.swing.JDialog {

    private final static long serialVersionUID = 1232131233134L;
    private MonopoliView frameView;

    /** Creates new form JSettingInitializationServer */
    public JSettingInitializationServer(java.awt.Frame parent, boolean modal, MonopoliView fv) {
        super(parent, modal);
        frameView = fv;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMonopoliJ = new javax.swing.JLabel();
        lblInizioPartita = new javax.swing.JLabel();
        cmdImpostaServer = new javax.swing.JButton();
        lblIpServer = new javax.swing.JLabel();
        txtIpServer = new javax.swing.JTextField();
        lblPorta = new javax.swing.JLabel();
        txtPorta = new javax.swing.JTextField();
        lblEsempioIp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        lblMonopoliJ.setFont(new java.awt.Font("Segoe Print", 1, 36));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(monopoli.MonopoliApp.class).getContext().getResourceMap(JSettingInitializationServer.class);
        lblMonopoliJ.setText(resourceMap.getString("lblMonopoliJ.text")); // NOI18N
        lblMonopoliJ.setName("lblMonopoliJ"); // NOI18N

        lblInizioPartita.setFont(resourceMap.getFont("lblInizioPartita.font")); // NOI18N
        lblInizioPartita.setText(resourceMap.getString("lblInizioPartita.text")); // NOI18N
        lblInizioPartita.setFocusCycleRoot(true);
        lblInizioPartita.setName("lblInizioPartita"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(monopoli.MonopoliApp.class).getContext().getActionMap(JSettingInitializationServer.class, this);
        cmdImpostaServer.setAction(actionMap.get("setInitializationServer")); // NOI18N
        cmdImpostaServer.setFont(resourceMap.getFont("cmdImpostaServer.font")); // NOI18N
        cmdImpostaServer.setText(resourceMap.getString("cmdImpostaServer.text")); // NOI18N
        cmdImpostaServer.setName("cmdImpostaServer"); // NOI18N
        cmdImpostaServer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cmdImpostaServerMouseClicked(evt);
            }
        });

        lblIpServer.setFont(resourceMap.getFont("lblIpServer.font")); // NOI18N
        lblIpServer.setText(resourceMap.getString("lblIpServer.text")); // NOI18N
        lblIpServer.setName("lblIpServer"); // NOI18N

        txtIpServer.setText(resourceMap.getString("txtIpServer.text")); // NOI18N
        txtIpServer.setToolTipText(resourceMap.getString("txtIpServer.toolTipText")); // NOI18N
        txtIpServer.setName("txtIpServer"); // NOI18N

        lblPorta.setFont(resourceMap.getFont("lblPorta.font")); // NOI18N
        lblPorta.setText(resourceMap.getString("lblPorta.text")); // NOI18N
        lblPorta.setName("lblPorta"); // NOI18N

        txtPorta.setText(resourceMap.getString("txtPorta.text")); // NOI18N
        txtPorta.setToolTipText(resourceMap.getString("txtPorta.toolTipText")); // NOI18N
        txtPorta.setName("txtPorta"); // NOI18N

        lblEsempioIp.setText(resourceMap.getString("lblEsempioIp.text")); // NOI18N
        lblEsempioIp.setName("lblEsempioIp"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(lblInizioPartita)
                            .addComponent(lblMonopoliJ)
                            .addComponent(cmdImpostaServer, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblIpServer)
                            .addComponent(lblPorta))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtIpServer, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblEsempioIp))
                            .addComponent(txtPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblMonopoliJ, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblInizioPartita)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIpServer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblEsempioIp)
                    .addComponent(txtIpServer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPorta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPorta))
                .addGap(30, 30, 30)
                .addComponent(cmdImpostaServer, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdImpostaServerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdImpostaServerMouseClicked

        Communication com = Monopoli.getMonopoliCommunication();
        boolean ret;

        /*   Lettura da file INI
        HashMap parameters=null;
        String iniFileName = "Parameters.ini";
        IniParser iniFile=null;
        try {
            iniFile = new IniParser("Parameters.ini");
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        parameters = new HashMap();
        ip = iniFile.getValue("SERVER","ip");
        port = Integer.parseInt(iniFile.getValue("SERVERD", "port"));
        */

        String ip = txtIpServer.getText();
        int port = Integer.parseInt(txtPorta.getText());

        ret = com.setInitializationServer(txtIpServer.getText(), Integer.parseInt(txtPorta.getText()));

        if (ret) {
            
            //Aggiorna la tab del Dati Server
            Monopoli.getMonopoliGUI().getGestoreRefresh().refreshInitializationServerData(txtIpServer.getText(), txtPorta.getText());

            //Aggiorna la DebugConsole
            Monopoli.getMonopoliGUI().getGestoreRefresh().refreshDebugConsole(
                    "[DBG_gui] InitializationServer " + txtIpServer.getText() +
                    " sulla porta " + txtPorta.getText() +
                    " impostato correttamente");
        }
        else {
            //Aggiorna la DebugConsole
            Monopoli.getMonopoliGUI().getGestoreRefresh().refreshDebugConsole(
                    "[DBG_gui] Impostazione dell'InitializationServer fallita");
        }

        this.dispose();
    }//GEN-LAST:event_cmdImpostaServerMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JSettingInitializationServer dialog = new JSettingInitializationServer(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdImpostaServer;
    private javax.swing.JLabel lblEsempioIp;
    private javax.swing.JLabel lblInizioPartita;
    private javax.swing.JLabel lblIpServer;
    private javax.swing.JLabel lblMonopoliJ;
    private javax.swing.JLabel lblPorta;
    private javax.swing.JTextField txtIpServer;
    private javax.swing.JTextField txtPorta;
    // End of variables declaration//GEN-END:variables
}
